<?php
/**
 * Description: 同步用户数据
 * User: devforma
 * Date: 16/3/9
 * Time: 17:56
 */

class SyncUserData implements iController {

    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {
        if (validateUser($queryArgs) !== 1)
            return genOutput(1000);

        //用户 - 每日一题列表
        $topics = $this->getUserTopics($queryArgs['user_id']);

        //用户 - 时政列表
        $politics = $this->getUserPolitics($queryArgs['user_id']);

        //用户 - 每日一面列表
        $interviews = $this->getUserInterviews($queryArgs['user_id']);

        $topicList = array();
        foreach ($topics as $t) {
            $topicList[] = array(
                'id' => intval($t->ID),
                'title' => trim(strtok($t->post_title, '-')),
                'is_done' => !empty($t->user_answer),
                'done_time' => empty($t->wrong_time) ? '' : $t->wrong_time,
                'is_collect' => !empty($t->is_store),
                'collect_time' => empty($t->store_time) ? '' : $t->store_time,
                'is_right' => $t->is_wrong != 1
            );
        }

        $politicList = array();
        foreach ($politics as $p) {
            $politicList[] = array(
                'id' => intval($p->politic_id),
                'title' => $p->politic_name,
                'is_collected' => !empty($p->is_collected),
                'done_time' => $p->read_time,
                'collect_time' => empty($p->collect_time) ? '' : $p->collect_time
            );
        }

        $interviewList = array();
        foreach ($interviews as $i) {
            $interviewList[] = array(
                'id' => self::getInterviewId($i->qid),
                'question_id' => intval($i->qid),
                'subject' => $i->subject,
                'is_done' => !empty($i->status),
                'is_collected' => !empty($i->is_collected),
                'collect_time' => empty($i->collect_time) ? '' : $i->collect_time
            );
        }

        return genOutput(1, array(
            'topics' => $topicList,
            'politics' => $politicList,
            'interviews' => $interviewList
        ));
    }

    /**
     * 获取用户 - 每日一题列表
     *
     * @param $userId
     * @return array
     */
    private function getUserTopics($userId) {
        global $cmsdb;

        $sql = 'SELECT p.ID, p.post_title, r.* FROM `relationship_user_topic` AS r INNER JOIN `wp_posts` AS p
        ON r.topic_id=p.ID WHERE r.user_id=:uid ORDER BY p.ID DESC';

        return (array)$cmsdb->getResults($sql, array(':uid' => $userId));
    }

    /**
     * 获取用户 - 时政列表
     *
     * @param $userId
     * @return array
     */
    private function getUserPolitics($userId) {
        global $cmsdb;

        $sql = 'SELECT p.id AS politic_id, p.politic_name, up.* FROM `dl_user_politic` AS up INNER JOIN `data_politics` AS p
        ON up.politic_id=p.id AND up.user_id=:uid ORDER BY p.id DESC';

        return (array)$cmsdb->getResults($sql, array(':uid' => $userId));
    }

    /**
     * 获取用户 - 每日一面列表
     *
     * @param $userId
     * @return array
     */
    private function getUserInterviews($userId) {
        global $cmsdb;

        $sql = 'SELECT iu.qid, i.subject, iu.status, iu.is_collected, iu.collect_time FROM `data_interview_users` AS iu INNER JOIN `data_interview` AS i
        ON iu.qid=i.id AND iu.uid=:uid ORDER BY iu.id DESC';

        return (array)$cmsdb->getResults($sql, array(':uid' => $userId));
    }

    /**
     * 获取面试发布id
     *
     * @param int $questionId 题目id
     * @return int
     */
    private static function getInterviewId($questionId) {
        global $cmsdb;

        $cacheKey = 'func:getInterviewId:'.$questionId;
        if (($value = cacheGet($cacheKey)) == null) {
            $value = $cmsdb->getVar('SELECT `id` FROM `data_interview_userlist` WHERE `qid`=:qid ORDER BY `id` DESC LIMIT 1',
                array(':qid' => $questionId));

            cacheAdd($cacheKey, $value, 20*60);
        }

        return intval($value);
    }
}